<?php

	require_once('data.php');

	$start_init = 0;
	$limit_init = 10;

	$result = '<pre>#title</pre><p>#content</p>';	

	//Making model, controller
	if( isset($_POST['create']) )
	{
		$script = $_POST['script'];
		$type = $_POST['type'];

		$content = '';
		$file = '';
		$newfile = '';
		switch ($type) 
		{
			case 'model':				
				$file = str_replace('#filename', $script, $code_tpl['model']['file']);				
				$newfile = str_replace('#script', $script, $code_tpl['model']['location']);
				// replace table, start init, limit init time				
				$code_tpl['model']['content'] = str_replace('#classname', ucfirst($script)."_model", $code_tpl['model']['content']);
				$code_tpl['model']['content'] = str_replace('#table', $script."s", $code_tpl['model']['content']);
				$code_tpl['model']['content'] = str_replace('#startinit', $start_init, $code_tpl['model']['content']);
				$content = str_replace('#limitinit', $limit_init, $code_tpl['model']['content']);
				break;
			case '':
				break;
			default:				
				break;
		}
		
		$fh = fopen($file, 'w') or die("can't open file");	
		fwrite($fh, $content);

		if( copy($file, $newfile) )
		{
			$result = str_replace('#title', 'Making script done!', $result);
			$result = str_replace('#content', 'Location: '. $newfile, $result);
		}
	}
	else
	{
		// init
		$result = str_replace('#title', 'Start making script', $result);
		$result = str_replace('#content', '..', $result);
	}	
?>

<html>
	<head>
		<title>Making Script</title>
	</head>

	<body>
	<form action="making_script.php" method="post">
		<fieldset>
			<legend>Making Script</legend>	
			<label>Script Name</label>
			<input type="text" name="script" value="" />
			&nbsp; (*) no capitalized
			<br/>
			<label>Type</label>
			<select name="type">
				<option value="model">Model</option>
				<option value="controller">Controller</option>
			</select>
			<br/>
			<input type="submit" name="create" value="Make Script">
		</fieldset>
	</form>
	<?php echo $result; ?>
	</body>
</html>